package com.shangcheng.server.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.shangcheng.model.domain.OmsOrder;
import com.shangcheng.model.domain.OmsOrderSetting;
import com.shangcheng.server.mapper.OmsOrderMapper;
import com.shangcheng.server.mapper.OmsOrderSettingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JavaClimber
 * @version 1.0
 * @date 2022/5/18 0018 zihao
 */
//定时查询秒杀,如果是秒杀,就查询订单和现在的时间.如果时间大于设置的时间,就关闭
@Component
@Slf4j
public class OmsTimedTask {
    @Autowired
    private OmsOrderMapper omsOrderMapper;
    @Autowired
    private OmsOrderSettingMapper omsOrderSettingMapper;
    
    @Scheduled(cron = "0/5 * * * * ?")
    public void an() {
        log.info("正在自动执行------");
        //从条件中查询到所有的订单集合
        List<OmsOrder> omsOrders = omsOrderMapper.selectList(null);
        OmsOrderSetting omsOrderSetting = omsOrderSettingMapper.selectById(1);
        //秒杀订单设置时间(分钟)
        Integer miaoTime = omsOrderSetting.getFlashOrderOvertime();
        //正常订单设置时间(分钟)
        Integer normalTime = omsOrderSetting.getNormalOrderOvertime();
        //发货超过时间间隔(天)
        Integer confirmOvertime = omsOrderSetting.getConfirmOvertime();
        //订单完成超过(天)
        Integer finishOvertime = omsOrderSetting.getFinishOvertime();
        //订单完成超过(天)
        Integer commentOvertime = omsOrderSetting.getCommentOvertime();
        
        //从订单集合中找到所有的秒杀订单,如果超过查询到的时间未付款,就关闭,正常订单,发货时间,订单完成时间
        //先找各个订单项,例如所有的秒杀订单
        List<OmsOrder> miaoShaDingDan = new ArrayList<>();
        
        for (OmsOrder omsOrder : omsOrders) {
            //判断当前订单中的秒杀订单设置时间(分钟)和现在的时间间隔
            //获取表中提交时间
            DateTime createTime = DateUtil.parse(omsOrder.getCreateTime());
            //获取现在的时间
            DateTime today = DateUtil.parse(DateUtil.today());
            //获取到日期时间差
            long betweenTime = DateUtil.between(createTime, today, DateUnit.MINUTE);
            
            //获取表中发货时间
            DateTime fahuoTime = DateUtil.parse(omsOrder.getDeliveryTime());
            //计算发货时间和现在的时间差
            long betweenTime2 = DateUtil.between(createTime, fahuoTime, DateUnit.DAY);
    
            //如果是秒杀订单,又是未付款订单,又是超时订单或者又是正常订单,又是又是未付款订单,又是超时订单
            if ((omsOrder.getOrderType() == 1L && omsOrder.getStatus() == 0L && betweenTime > miaoTime) ||
                    (omsOrder.getOrderType() == 0L && omsOrder.getStatus() == 0L && betweenTime > normalTime)) {
                //就将更新这条数据status为未无效订单5
                omsOrder.setStatus(5L);
                //将数据更新
                omsOrderMapper.updateById(omsOrder);
                log.info("id为" + omsOrder.getId() + "的订单超时,后台自动执行订单关闭任务");
            }
            //如果发货时间差大于表中我们设置的时间差
            else if (betweenTime2>confirmOvertime){
                //就将更新这条数据status为未完成单3
                omsOrder.setStatus(3L);
                //将数据更新
                omsOrderMapper.updateById(omsOrder);
                log.info("id为" + omsOrder.getId() + "的订单超过"+confirmOvertime+"天.后台自动执行订单完成任务");
            }
           
            
        }
    }
    
}
